package com.cn.ssm.utils.files;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cn.ssm.model.Ucell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

    public class ExcelTools {
        private static final String EXCEL_XLS = "xls";
        private static final String EXCEL_XLSX = "xlsx";
        private static ExcelTools et=new ExcelTools();


        public static ExcelTools getSigleObj(){
            return et;
        }



        public List<Object> readExcelFile(String filepath){
            List<Object> results=new ArrayList<Object>();


            SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
//       BufferedWriter bw = new BufferedWriter(new FileWriter(new File("E:/xxx/InsertSql.txt")));
            try {

                File excelFile = new File(filepath);
                FileInputStream is = new FileInputStream(excelFile);
                checkExcelVaild(excelFile);
                Workbook workbook = getWorkbok(is,excelFile);
                int sheetCount = workbook.getNumberOfSheets();

                Sheet sheet = workbook.getSheetAt(0);


                int count = 0;

                for (Row row : sheet) {

                    if(count == 0){
                        count++;
                        continue;
                    }

                    String rowValue = "";
                    Map<String,Object> mp=new HashMap<String,Object>();
                    int i=0;
                    for (Cell cell : row) {
                        if(cell.toString() == null||"".equals(cell.toString())){
                            continue;
                        }
                        int cellType = cell.getCellType();
                        String cellValue = "";
                        switch (cellType) {
                            case Cell.CELL_TYPE_STRING:
                                cellValue = cell.getRichStringCellValue().getString() + "#";
                                break;
                            case Cell.CELL_TYPE_NUMERIC:
                                if (HSSFDateUtil.isCellDateFormatted(cell)) {
                                    cellValue = fmt.format(cell.getDateCellValue()) + "#";
                                } else {
                                    cell.setCellType(Cell.CELL_TYPE_STRING);
                                    cellValue = String.valueOf(cell.getRichStringCellValue().getString()) + "#";
                                }
                                break;
                            case Cell.CELL_TYPE_BOOLEAN:
                                cellValue = String.valueOf(cell.getBooleanCellValue()) + "#";
                                break;
                            case Cell.CELL_TYPE_BLANK:
                                cellValue = cell.getStringCellValue() + "#";
                                break;
                            case Cell.CELL_TYPE_ERROR:
                                cellValue = "";
                                break;
                            case Cell.CELL_TYPE_FORMULA:

                                //cellValue = cell.getCellFormula() + "#";

                                cell.setCellType(Cell.CELL_TYPE_STRING);
                                cellValue = String.valueOf(cell.getRichStringCellValue().getString()) + "#";
                                break;
                            default:
                                cellValue = "";
                        }
                        System.out.println(count+"�У���"+i+"�е������ǣ�"+cellValue);
                        rowValue += cellValue;
                        mp.put("obj"+i,cellValue.replace("#",""));
                        i++;

                    }
                    if(mp!=null){
                        results.add(mp);
                    }
                    count++;
                }


            } catch (Exception e) {
                e.printStackTrace();
            }
            return results;

        }



        public static Workbook getWorkbok(InputStream in,File file) throws IOException{
            Workbook wb = null;
            if(file.getName().endsWith(EXCEL_XLS)){
                wb = new HSSFWorkbook(in);
            }
            return wb;
        }


        public static void checkExcelVaild(File file) throws Exception{
            if(!file.exists()){
                throw new Exception("文件不存在");
            }
            if(!(file.isFile() && (file.getName().endsWith(EXCEL_XLS) || file.getName().endsWith(EXCEL_XLSX)))){
                throw new Exception("文件类型不对");
            }
        }


        public static boolean updateCellValue(List<Ucell> cellvalues, String filepath) {
            SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
            try {
                // 支持Excel 2003和2007
                File excelFile = new File(filepath);
                FileInputStream is = new FileInputStream(excelFile);
                checkExcelVaild(excelFile);
                Workbook workbook = getWorkbok(is, excelFile);
                is.close();
                Sheet sheet = workbook.getSheetAt(0);
                for (Ucell ucell : cellvalues) {
                    Row row = sheet.getRow(ucell.getRows());
                    Cell cell1 = row.getCell(ucell.getColumns());
                    cell1.setCellType(Cell.CELL_TYPE_STRING);
                    cell1.setCellValue(ucell.getCellvalue().toString());

                }


                FileOutputStream fs = new FileOutputStream(excelFile);
                workbook.write(fs);
                fs.flush();
                fs.close();
                System.out.println("excel更新单元格完毕");

            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }




    }
